package com.android.fastride.model.http;

import com.android.fastride.FastRideApplication;
import com.android.fastride.model.http.intercepter.ApiInterceptor;
import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.franmontiel.persistentcookiejar.ClearableCookieJar;
import com.franmontiel.persistentcookiejar.PersistentCookieJar;
import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;
import com.peng.android.coremodule.http.convert.FastJsonConverterFactory;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;

/**
 * 网络请求客户端
 * Created by pyt on 2017/7/5.
 */

public class HttpApiClient {

	private static final String TAG = "HttpApiClient";

	/**
	 * 创建一个默认的OkHttp请求构造器
	 *
	 * @return
	 */
	public static OkHttpClient.Builder createDefaultOkHttpClientBuilder () {
		OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();
		okHttpBuilder.connectTimeout(10, TimeUnit.SECONDS);
		okHttpBuilder.readTimeout(5, TimeUnit.SECONDS);
		okHttpBuilder.writeTimeout(5, TimeUnit.SECONDS);
		if (FastRideApplication.getApp().isDebug()) {
			okHttpBuilder.addNetworkInterceptor(new StethoInterceptor());
		}
		return okHttpBuilder;
	}

	/**
	 * 根据不同的URL和Service clz对象，创建指定的Service
	 *
	 * @param baseUrl
	 * @param serviceClz
	 * @param <T>
	 * @return
	 */
	public static <T> T createApiService (String baseUrl, Class<T> serviceClz) {
		ClearableCookieJar cookieJar =
				new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(FastRideApplication.getApp()));
		//创建一个默认的okHttpClient
		OkHttpClient okHttpClient = createDefaultOkHttpClientBuilder()
				.cookieJar(cookieJar)
				.addInterceptor(new ApiInterceptor()).build();
		//初始化Retrofit
		Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
		//添加Soap请求的转换器
		Retrofit retrofit = retrofitBuilder
				.addConverterFactory(FastJsonConverterFactory.create())
				.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
				.client(okHttpClient)
				.baseUrl(baseUrl)
				.build();
		return retrofit.create(serviceClz);
	}

	/**
	 * 根据不同的URL和Service clz对象，创建指定的Service
	 *
	 * @param baseUrl
	 * @param serviceClz
	 * @param <T>
	 * @return
	 */
	public static <T> T createApiServiceWithTimeout (String baseUrl, Class<T> serviceClz,long connectTimeout,long readTimeout,long writeTimeout) {
		ClearableCookieJar cookieJar =
				new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(FastRideApplication.getApp()));
		//创建一个默认的okHttpClient
		OkHttpClient okHttpClient = createDefaultOkHttpClientBuilder()
				.cookieJar(cookieJar)
				.connectTimeout(connectTimeout, TimeUnit.SECONDS)
				.readTimeout(readTimeout, TimeUnit.SECONDS)
				.writeTimeout(writeTimeout,TimeUnit.SECONDS)
				.addInterceptor(new ApiInterceptor()).build();
		//初始化Retrofit
		Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
		//添加Soap请求的转换器
		Retrofit retrofit = retrofitBuilder
				.addConverterFactory(FastJsonConverterFactory.create())
				.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
				.client(okHttpClient)
				.baseUrl(baseUrl)
				.build();
		return retrofit.create(serviceClz);
	}



}
